#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
#include <time.h>
#include <limits.h>
#include <stdalign.h>
#include <tgmath.h>
#include "h/names.h"

#define LEN 81
#define SWAP(type, a, b)    do {type c=a; a=b; b=c;} while(0)

#define RAD_TO_DEG (180.0/( 4*atan(1) ))

#define SQRT(x) _Generic((x), \
    float: sqrtf,\
    long double: sqrtl,\
    default: sqrt\
    )(x)

#define SIN(x) _Generic((x),\
    long double: sinl((x)/RAD_TO_DEG),\
    default: sin((x)/RAD_TO_DEG),\
    float: sinf((x)/RAD_TO_DEG)\
    )

typedef struct {
    double magnitude;
    double angle;
} Polar_V;

typedef struct {
     double x;
     double y;
} Rect_V;



Polar_V rect_to_polar(Rect_V);

char * s_gets(char *, int);
void eatline(void);

int main(void)
{

    float x  = 45.0f;
    double xx = 45.0;
    long double xxx = 45.0L;

    long double y = SQRT(x);
    long double yy = SQRT(xx);
    long double yyy = SQRT(xxx);

    printf("%.17Lf\n", y);
    printf("%.17Lf\n", yy);
    printf("%.17Lf\n", yyy);

    int i = 45;

    yy = SQRT(i);
    printf("%.17Lf\n", yy);
    yyy = SIN(xxx);
    printf("%.17Lf\n", yyy);

    printf("use tgmath.h:\n");
    printf("%.17f\n", sqrt(x));
    printf("%.17lf\n", sqrt(xx));
    printf("%.17Lf\n", sqrt(xxx));
    printf("%.17lf\n", (sqrt)(x));
    printf("%.17lf\n", (sqrt)(xx));
    printf("%.17lf\n", (sqrt)(xxx));


    return 0;
}

Polar_V rect_to_polar(Rect_V rv)
{
    Polar_V pv;
    pv.magnitude = sqrt(rv.x * rv.x + rv.x * rv.x);
    if (pv.magnitude == 0)
        pv.angle = 0.0;
    else
        pv.angle = RAD_TO_DEG * atan2(rv.y, rv.x);

    return pv;
}

char * s_gets(char * str, int n)
{
    char * ret_val;
    char * find;

    if ((ret_val = fgets(str, n, stdin))) {
        find = strchr(str, '\n');
        if (find)
            *find = 0;
        else
            while (getchar() != '\n')
                ;
    }

    return ret_val;
}

void eatline(void)
{
    while (getchar() != '\n')
        ;
}
